package cn.wygandwdn.core.transport.server.handler;

import cn.wygandwdn.common.constant.RpcConstants;
import cn.wygandwdn.common.message.PingMessage;
import cn.wygandwdn.common.message.PongMessage;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description:
 * @author: WYG
 * @time: 2021/10/28 19:33
 */

public class RpcServerPingHandler extends SimpleChannelInboundHandler<PingMessage> {

    private static final Logger log = LoggerFactory.getLogger(RpcServerPingHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, PingMessage msg) throws Exception {
        log.debug("收到客户端心跳包: {}", msg.getPing());
        PongMessage pong = new PongMessage.Builder(0L).pong(RpcConstants.PONG).build();
        ctx.writeAndFlush(pong).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
    }

}
